/***********************************************************************
 * Module:  ListeCouleur.java
 * Author:  Fabien Muguet
 * Purpose: Defines the Class ListeCouleur
 ***********************************************************************/

package vue.composants;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 *
 * @author sylvainc
 */
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.color.ColorSpace;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import modele.Ressources;


public class ListeCouleur extends JComboBox {

        private ListeCouleurEcouteur target;
        private int w = 100;
        private int h = 20;
	private MyColor[] colors;
        private Color couleurCourante;
        private Ressources ressources;// = new Ressources();
        private boolean estSelectionne;

	public ListeCouleur(Ressources ressouces, ListeCouleurEcouteur target) {
                this.ressources = ressouces;
                this.target = target;
		initializeCombo();
                estSelectionne = true;
	}
        public ListeCouleur(Ressources ressouces, ListeCouleurEcouteur target,int w, int h) {
                this.ressources = ressouces;
                this.target = target;
                this.w = w;
                this.h = h;
		initializeCombo();
                estSelectionne = true;
	}

	private void initializeCombo() {
		colors = new MyColor[ressources.getCouleurs().size()];
                for(int i=0;i<ressources.getCouleurs().size();i++)
                {
                    colors[i] =  new MyColor(ressources.getCouleurs().elementAt(i).getRGB(),""); // On recup pas le nom des couleurs (voir chargeur)
                }
		DefaultComboBoxModel model = new DefaultComboBoxModel(colors);
		setModel(model);
                couleurCourante = colors[0];
		setRenderer(new ListCellRenderer() {
			public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {

                                if(estSelectionne)
                                    setBackground( new Color(255,255,128) );
                                else
                                    setBackground( new Color(238,238,238) );
				MyColor myColor = (MyColor) value;
				BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
				Graphics g = img.getGraphics();
				g.setColor(myColor);
				g.fillRect(0, 0, w, h);
				ImageIcon icon = new ImageIcon(img);
				JLabel label = new JLabel(icon, JLabel.LEADING);
				return label;
			}
		});
		addActionListener(new ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				JComboBox source = (JComboBox)e.getSource();
				couleurCourante = (MyColor)source.getSelectedItem();
                                target.couleurChangee(couleurCourante);
			};
		});     
	}

        public void setSelectionne(boolean estSelectionne) {
            this.estSelectionne = estSelectionne;
            repaint();
        }

        public Color getCouleurCourante(){
            return this.couleurCourante;
            }

	class MyColor extends Color {
		private String colorName;

		public MyColor(int rgb, String colorName) {
			super(rgb);
			this.colorName = colorName;
		}

		public MyColor(int rgba, boolean hasalpha, String colorName) {
			super(rgba, hasalpha);
			this.colorName = colorName;
		}

		public MyColor(int r, int g, int b, String colorName) {
			super(r, g, b);
			this.colorName = colorName;
		}

		public MyColor(float r, float g, float b, String colorName) {
			super(r, g, b);
			this.colorName = colorName;
		}

		public MyColor(ColorSpace cspace, float[] components, float alpha, String colorName) {
			super(cspace, components, alpha);
			this.colorName = colorName;
		}

		public MyColor(int r, int g, int b, int a, String colorName) {
			super(r, g, b, a);
			this.colorName = colorName;
		}

		public MyColor(float r, float g, float b, float a, String colorName) {
			super(r, g, b, a);
			this.colorName = colorName;
		}

		public String getColorName() {
			return colorName;
		}
	}

}
